package cn.oowoo.rabbitmq.confirm;

import cn.oowoo.rabbitmq.config.RabbitConfig;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author: Zhangbin
 * @Date: 11:23 2021/5/9
 * @Description: confirm模式批量确认测试-生产者
 **/
public class BatchConfirmProducer {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        Connection connection = RabbitConfig.createConnection();
        // 创建消息通道
        Channel channel = connection.createChannel();
        //开启发送方确认模式
        channel.confirmSelect();
        for(int i=0;i<3;i++){
            String msg = "测试confirm消息.... "+i;
            channel.basicPublish("",RabbitConfig.QUEUE_CONFIRM_NAME,null,msg.getBytes());
        }
        /*
         批量确认结果:
            该方法会等到最后一条消息得到确认或者得到nack才会结束，也就是说在waitForConfirmsOrDie处会造成当前程序的阻塞
            只要有一个未被Broker确认就会IOException
         */
        channel.waitForConfirmsOrDie();

        //消息发送完毕关闭信道，关闭链接
        channel.close();
        connection.close();
    }
}
